package cn.itcast.Rainbow_self_mention.Server;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.util.Timer;
import java.util.TimerTask;

import RJZ.Rainbow.UserServer.entity.FileBlock;
import cn.itcast.Rainbow_self_mention.Activity.Detail_for_rec;
import cn.itcast.Rainbow_self_mention.Activity.Detail_for_sub;
import cn.itcast.Rainbow_self_mention.Activity.For_Upload_Order;
import cn.itcast.Rainbow_self_mention.Activity.Home;
import cn.itcast.Rainbow_self_mention.Activity.Order;
import cn.itcast.Rainbow_self_mention.Dao.SqlHelper;
import cn.itcast.Rainbow_self_mention.Tools.imfor;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
//提交订单服务


public class OrderServer extends Service {

    @Nullable
    SqlHelper hp = new SqlHelper(this);
    int userid;
    Channel ch;
    String ip = imfor.ip;
    static String imagepath;
    int port_of_message = 9527;
    Boolean link = false;
    Boolean link_for_send_picture = false;
    Timer timer,timer2;

    Handler Hhandler = Home.handler;
    Handler Ohandler = Order.handler;
    Handler Uphandler = For_Upload_Order.handler;
    Handler rec = Detail_for_rec.handler;
    Handler sub = Detail_for_sub.handler;
    Sender sender;
    /*
     * cop=1，获取投件订单；
     * cop=11,投件订单获取成功
     * cop=12，投件订单获取失败
     * cop=13，投件订单全部获取
     *
     * cop=2，获取取件订单；
     * cop=21，取件订单获取成功
     * cop=22，取件订单获取失败
     * cop=23，取件订单全部获取
     *
     * cop=3，投件人删除；
     * cop=31，投件人删除成功；
     * cop=32，投件人删除失败
     *
     * cop=4，取件人删除；
     * cop=41 取件人删除成功；
     * cop=42 取件人删除失败；
     *
     * cop=34，网络连接成功
     *
     * cop=5，提交订单；
     * cop=51,提交订单成功，准备接收图片
     * cop=52 订单和图片提交完成
     * cop=53 没有此用户，
     * cop=54 订单提交失败
     * cop=55 图片提交失败；
     *
     *
     */
    @Nullable
    @Override

    public IBinder onBind(Intent intent) {

        sender = new Sender();
        return sender;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        messageconnect();
        addTimer();
    }

    public void addTimer() {
        if (timer == null) {
            timer = new Timer();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    Bundle bundle = new Bundle();
                    Message h = Hhandler.obtainMessage();
                    Message od = Ohandler.obtainMessage();
                    Message up=Uphandler.obtainMessage();

                    if (link) {
                        bundle.putString("sentresult", "网络连接成功");
                    } else {
                        bundle.putString("sentresult", "网络连接错误");
                        //sender.relink();
                    }
                    h.setData(bundle);
                    od.setData(bundle);
                    up.setData(bundle);

                    Hhandler.sendMessage(h);
                    Ohandler.sendMessage(od);
                    Uphandler.sendMessage(up);

                }
            };
            timer.schedule(task, 500, 2000);
        }
    }


    private void messageconnect() {
        SQLiteDatabase db=hp.getReadableDatabase();
        Cursor cursor=db.query("logined",null,null,null,null,null,null);
        cursor.moveToFirst();
        userid=cursor.getInt(cursor.getColumnIndex("userid"));
        cursor.close();
        db.close();
        NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture bs = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LineBasedFrameDecoder(2048));
                        ch.pipeline().addLast(new StringEncoder());
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast("input", new ChannelInboundHandlerAdapter() {
                            public void channelActive(ChannelHandlerContext ctx) {
                                String build = "{" +
                                        "\"Cop\":" + "\"RJZ\","
                                        + "\"userid\":" + "\"" + userid + "\""
                                        + "}" + '\n';
                                ctx.channel().writeAndFlush(build);
                                link = true;
                                Bundle bundle = new Bundle();
                                Message h = Hhandler.obtainMessage();
                                Message od = Ohandler.obtainMessage();
                                Message up=Uphandler.obtainMessage();
                                bundle.putString("sentresult", "网络连接成功");
                                h.setData(bundle);
                                od.setData(bundle);
                                up.setData(bundle);
                                Hhandler.sendMessage(h);
                                Ohandler.sendMessage(od);
                                Uphandler.sendMessage(up);
                            }
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                super.channelRead(ctx, msg);
                                Log.i("收到消息",msg.toString());
                                operation(msg);

                            }
                            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                ctx.fireChannelInactive();
                                link = false;
                                Bundle bundle = new Bundle();
                                Message h = Hhandler.obtainMessage();
                                Message od = Ohandler.obtainMessage();
                                Message up=Uphandler.obtainMessage();
                                bundle.putString("sentresult", "网络连接错误");
                                h.setData(bundle);
                                od.setData(bundle);
                                up.setData(bundle);
                                Hhandler.sendMessage(h);
                                Ohandler.sendMessage(od);
                                Uphandler.sendMessage(up);
                                sender.relink();
                            }
                        });
                    }
                })
                .connect(new InetSocketAddress(ip, port_of_message));
        ch = bs.channel();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ChannelFuture cf = ch.closeFuture();
        try {
            cf.sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public class Sender extends Binder {
        FileBlock fileBlock=new FileBlock();

        public void relink() {
            messageconnect();
        }

        public void gain_send(int userid) {

            String msg = "{" +
                    "\"Cop\":" + "\"1\"," +
                    "\"userid\":" + "\"" + userid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);
        }

        public void gain_received(int userid) {

            String msg = "{" +
                    "\"Cop\":" + "\"2\"," +
                    "\"userid\":" + "\"" + userid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);
        }

        public void get_take_code(String userid,String orderid){

            String msg = "{" +
                    "\"Cop\":" + "\"81\"," +
                    "\"userid\":" + "\"" + userid + "\"," +
                    "\"orderid\":" + "\"" + orderid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);

        }

        public void get_sub_code(String userid,String orderid){

            String msg = "{" +
                    "\"Cop\":" + "\"82\"," +
                    "\"userid\":" + "\"" + userid + "\"," +
                    "\"orderid\":" + "\"" + orderid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);

        }

        public void sub_delete(String orderid,String userid) {
            String msg = "{" +
                    "\"Cop\":" + "\"3\"," +
                    "\"userid\":" + "\"" + userid + "\"," +
                    "\"orderid\":" + "\"" + orderid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);
        }

        public void rec_delete(String orderid,String userid) {
            String msg = "{" +
                    "\"Cop\":" + "\"4\"," +
                    "\"userid\":" + "\"" + userid + "\"," +
                    "\"orderid\":" + "\"" + orderid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);
        }

        public void send_order(int userid, String name_of_good, String receiver_number, String name_of_picture, String time_of_submit, int boxid, String imagepath) {
            String msg = "{" +
                    "\"Cop\":" + "\"5\"," +
                    "\"userid\":" + "\"" + userid + "\"," +
                    "\"name_of_good\":" + "\"" + name_of_good + "\"," +
                    "\"receiver_number\":" + "\"" + receiver_number + "\"," +
                    "\"name_of_picture\":" + "\"" + name_of_picture + "\"," +
                    "\"time_of_submit\":" + "\"" + time_of_submit + "\"," +
                    "\"boxid\":" + "\"" + boxid + "\"" +
                    "}" + '\n';
            ch.writeAndFlush(msg);
            OrderServer.imagepath = imagepath;
        }

        public void send_picture(int upport) {
            fileBlock.set(imagepath);
            NioEventLoopGroup group = new NioEventLoopGroup();
            ChannelFuture bs = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<NioSocketChannel>() {
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectEncoder());
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(null)));
                            ch.pipeline().addLast("input", new ChannelInboundHandlerAdapter() {
                                public void channelActive(ChannelHandlerContext ctx) throws IOException {
                                    link_for_send_picture = true;
                                    fileBlock.randomAccessFile = new RandomAccessFile(fileBlock.filePacket.getFile(), "r");
                                    fileBlock.randomAccessFile.seek(fileBlock.filePacket.getStartPos());
                                    fileBlock.lastLength = 1048576;   //每次发送的文件块数的长度
                                    byte[] bytes = new byte[fileBlock.lastLength];

                                    if ((fileBlock.byteRead = fileBlock.randomAccessFile.read(bytes)) != -1) {
                                        fileBlock.filePacket.setEndPos(fileBlock.byteRead);
                                        fileBlock.filePacket.setBytes(bytes);
                                        ctx.writeAndFlush(fileBlock.filePacket);
                                    } else {
                                        fileBlock.lastLength = 0;
                                    }
                                    Message od = Ohandler.obtainMessage();
                                    Bundle bundle = new Bundle();
                                    bundle.putString("sentresult", "正在发送图片");
                                    od.setData(bundle);
                                    Ohandler.sendMessage(od);
                                }
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    super.channelRead(ctx, msg);
                                    if (msg instanceof Long) {
                                        fileBlock.start = (Long) msg;
                                        if (fileBlock.start != -1) {
                                            fileBlock.randomAccessFile = new RandomAccessFile(fileBlock.filePacket.getFile(), "r");
                                            fileBlock.randomAccessFile.seek(fileBlock.start);
                                           Long a = fileBlock.randomAccessFile.length() - fileBlock.start;
                                            int lastLength = 1048576;
                                            if (a < lastLength) {
                                                lastLength = a.intValue();
                                            }
                                            byte[] bytes = new byte[lastLength];
                                            if ((fileBlock.byteRead = fileBlock.randomAccessFile.read(bytes)) != -1 && (fileBlock.randomAccessFile.length() - fileBlock.start) > 0) {
                                                fileBlock.filePacket.setEndPos(fileBlock.byteRead);
                                                fileBlock.filePacket.setBytes(bytes);
                                                ctx.writeAndFlush(fileBlock.filePacket);
                                            } else {
                                                fileBlock.randomAccessFile.close();
                                                ctx.close();
                                                fileBlock.flush();
                                            }
                                        }
                                    }

                                }

                                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                    ctx.fireChannelInactive();
                                    link_for_send_picture = false;
                                }
                            });
                        }
                    })
                    .connect(new InetSocketAddress(ip, upport));
        }
    }

    //**************************************返回检验结果解析处*****************************
    public void operation(Object msg) throws JSONException {
        String m = msg.toString();
        JSONObject jsonObject = new JSONObject(m);
        Log.i("收到消息",m);
        if (jsonObject.getString("Cop").equals("11")) {
            SQLiteDatabase db = hp.getReadableDatabase();
            Cursor cursor = db.query("HISTORY", new String[]{"NUMBER"}, "NUMBER=?", new String[]{jsonObject.getString("NUMBER")}, null, null, null);
            cursor.moveToFirst();
            if(cursor.getCount()==0)
            {
                Log.i("正在执行----","投件订单插入");
                Log.i("NUMBER",jsonObject.getString("NUMBER"));
                cursor=db.query("HISTORY",null,null,null,null,null,null);
                cursor.moveToFirst();
                int a = cursor.getCount() + 1;
                Log.i("******库中总数******",(a-1)+"");
                db = hp.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("_id", a);
                values.put("NUMBER", jsonObject.getString("NUMBER"));
                values.put("NAME_OF_SUBMIT", jsonObject.getString("NAME_OF_SUBMIT"));
                values.put("PHONE_OF_SUBMIT", jsonObject.getString("PHONE_OF_SUBMIT"));
                values.put("TIME_OF_ORDER", jsonObject.getString("TIME_OF_ORDER"));
                values.put("TIME_OF_SUBMIT", jsonObject.getString("TIME_OF_SUBMIT"));
                values.put("NAME_OF_RECEIVER", jsonObject.getString("NAME_OF_RECEIVER"));
                values.put("PHONE_OF_RECEIVER", jsonObject.getString("PHONE_OF_RECEIVER"));
                values.put("TIME_OF_RECEIVER", jsonObject.getString("TIME_OF_RECEIVER"));
                values.put("LOCATION", jsonObject.getString("LOCATION"));
                values.put("TEMPERATURE", jsonObject.getString("TEMPERATURE"));
                values.put("NAME_OF_PRODUCTION", jsonObject.getString("NAME_OF_PRODUCTION"));
                values.put("NAME_OF_PICTURE", jsonObject.getString("NAME_OF_PICTURE"));
                values.put("RECEIVE_OR_NOT", jsonObject.getInt("RECEIVE_OR_NOT"));
                db.insert("HISTORY", null, values);
                db.close();
                cursor.close();
                Message home = Hhandler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("sentresult", "当前正在获取订单");
                home.setData(bundle);
                Hhandler.sendMessage(home);
                return;
            }
            if(cursor.getCount()==1)
            {
                Log.i("正在执行----","投件订单更新");
                Log.i("NUMBER",jsonObject.getString("NUMBER"));
                db = hp.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("NUMBER", jsonObject.getString("NUMBER"));
                values.put("NAME_OF_SUBMIT", jsonObject.getString("NAME_OF_SUBMIT"));
                values.put("PHONE_OF_SUBMIT", jsonObject.getString("PHONE_OF_SUBMIT"));
                values.put("TIME_OF_ORDER", jsonObject.getString("TIME_OF_ORDER"));
                values.put("TIME_OF_SUBMIT", jsonObject.getString("TIME_OF_SUBMIT"));
                values.put("NAME_OF_RECEIVER", jsonObject.getString("NAME_OF_RECEIVER"));
                values.put("PHONE_OF_RECEIVER", jsonObject.getString("PHONE_OF_RECEIVER"));
                values.put("TIME_OF_RECEIVER", jsonObject.getString("TIME_OF_RECEIVER"));
                values.put("TEMPERATURE", jsonObject.getString("TEMPERATURE"));
                values.put("LOCATION", jsonObject.getString("LOCATION"));
                values.put("NAME_OF_PRODUCTION", jsonObject.getString("NAME_OF_PRODUCTION"));
                values.put("NAME_OF_PICTURE", jsonObject.getString("NAME_OF_PICTURE"));
                values.put("RECEIVE_OR_NOT", jsonObject.getInt("RECEIVE_OR_NOT"));
                db.update("HISTORY",values,"NUMBER=?",new String[]{jsonObject.getString("NUMBER")});
                db.close();
                cursor.close();
                Message home = Hhandler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("sentresult", "当前正在获取订单");
                home.setData(bundle);
                Hhandler.sendMessage(home);
                return;
            }

            //sender.gain_picture();

        }
        if (jsonObject.getString("Cop").equals("12")) {
            Message home = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "获取订单失败");
            home.setData(bundle);
            Hhandler.sendMessage(home);

        }
        if (jsonObject.getString("Cop").equals("13")) {
            Message home = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "订单全部获取");
            home.setData(bundle);
            Hhandler.sendMessage(home);
        }
        if (jsonObject.getString("Cop").equals("34")) {
            link = true;
        }
        if (jsonObject.getString("Cop").equals("21")) {
            SQLiteDatabase db = hp.getReadableDatabase();
            Cursor cursor = db.query("HISTORY", new String[]{"NUMBER"}, "NUMBER=?", new String[]{jsonObject.getString("NUMBER")}, null, null, null);
            cursor.moveToFirst();
            if(cursor.getCount()==0)
            {
                Log.i("正在执行----","取件订单插入");
                Log.i("NUMBER",jsonObject.getString("NUMBER"));
                cursor=db.query("HISTORY",null,null,null,null,null,null);
                cursor.moveToFirst();
                int a = cursor.getCount() + 1;
                db = hp.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("_id", a);
                values.put("NUMBER", jsonObject.getString("NUMBER"));
                values.put("NAME_OF_SUBMIT", jsonObject.getString("NAME_OF_SUBMIT"));
                values.put("PHONE_OF_SUBMIT", jsonObject.getString("PHONE_OF_SUBMIT"));
                values.put("TIME_OF_ORDER", jsonObject.getString("TIME_OF_ORDER"));
                values.put("TIME_OF_SUBMIT", jsonObject.getString("TIME_OF_SUBMIT"));
                values.put("NAME_OF_RECEIVER", jsonObject.getString("NAME_OF_RECEIVER"));
                values.put("PHONE_OF_RECEIVER", jsonObject.getString("PHONE_OF_RECEIVER"));
                values.put("TIME_OF_RECEIVER", jsonObject.getString("TIME_OF_RECEIVER"));
                values.put("LOCATION", jsonObject.getString("LOCATION"));
                values.put("TEMPERATURE", jsonObject.getString("TEMPERATURE"));
                values.put("NAME_OF_PRODUCTION", jsonObject.getString("NAME_OF_PRODUCTION"));
                values.put("NAME_OF_PICTURE", jsonObject.getString("NAME_OF_PICTURE"));
                values.put("RECEIVE_OR_NOT", jsonObject.getInt("RECEIVE_OR_NOT"));
                db.insert("HISTORY", null, values);
                db.close();
                cursor.close();
                Message home = Hhandler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("sentresult", "当前正在获取订单");
                home.setData(bundle);
                Hhandler.sendMessage(home);
                return;
            }
            if(cursor.getCount()==1)
            {
                Log.i("正在执行----","取件订单更新");
                Log.i("NUMBER",jsonObject.getString("NUMBER"));
                db = hp.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("NUMBER", jsonObject.getString("NUMBER"));
                values.put("NAME_OF_SUBMIT", jsonObject.getString("NAME_OF_SUBMIT"));
                values.put("PHONE_OF_SUBMIT", jsonObject.getString("PHONE_OF_SUBMIT"));
                values.put("TIME_OF_ORDER", jsonObject.getString("TIME_OF_ORDER"));
                values.put("TIME_OF_SUBMIT", jsonObject.getString("TIME_OF_SUBMIT"));
                values.put("NAME_OF_RECEIVER", jsonObject.getString("NAME_OF_RECEIVER"));
                values.put("PHONE_OF_RECEIVER", jsonObject.getString("PHONE_OF_RECEIVER"));
                values.put("TIME_OF_RECEIVER", jsonObject.getString("TIME_OF_RECEIVER"));
                values.put("LOCATION", jsonObject.getString("LOCATION"));
                values.put("TEMPERATURE", jsonObject.getString("TEMPERATURE"));
                values.put("NAME_OF_PRODUCTION", jsonObject.getString("NAME_OF_PRODUCTION"));
                values.put("NAME_OF_PICTURE", jsonObject.getString("NAME_OF_PICTURE"));
                values.put("RECEIVE_OR_NOT", jsonObject.getInt("RECEIVE_OR_NOT"));
                db.update("HISTORY",values,"NUMBER=?",new String[]{jsonObject.getString("NUMBER")});
                db.close();
                cursor.close();
                Message home = Hhandler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("sentresult", "当前正在获取订单");
                home.setData(bundle);
                Hhandler.sendMessage(home);
                return;
            }
            //sender.gain_picture();
        }
        if (jsonObject.getString("Cop").equals("22")) {
            Message home = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "获取订单失败");
            home.setData(bundle);
            Hhandler.sendMessage(home);
        }
        if (jsonObject.getString("Cop").equals("23")) {
            Message home = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "订单全部获取");
            home.setData(bundle);
            Hhandler.sendMessage(home);
        }
        if (jsonObject.getString("Cop").equals("31")) {

            Message lfo = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "投件删除成功");
            lfo.setData(bundle);
            Hhandler.sendMessage(lfo);
        }
        if (jsonObject.getString("Cop").equals("32")) {
            Message lfo =Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "投件删除失败");
            lfo.setData(bundle);
            Hhandler.sendMessage(lfo);
        }
        if (jsonObject.getString("Cop").equals("41")) {
            SQLiteDatabase db = hp.getWritableDatabase();
            db.delete("HISTORY", "NUMBER=?", new String[]{jsonObject.getString("NUMBER") });
            db.close();
            Message lfr = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "取件删除成功");
            lfr.setData(bundle);
            Hhandler.sendMessage(lfr);
        }
        if (jsonObject.getString("Cop").equals("42")) {
            Message lfr = Hhandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "取件删除失败");
            lfr.setData(bundle);
            Hhandler.sendMessage(lfr);

        }
        if (jsonObject.getString("Cop").equals("51")) {
            sender.send_picture(jsonObject.getInt("port"));
            Log.i("订单提交完成，准备上传图片",m);
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "订单提交完成，准备上传图片");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("52")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "全部完成提交");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("53")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "没有此用户");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("54")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "订单提交失败");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("55")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "图片提交失败");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("56")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "正在发送图片");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("57")) {
            Message od = Uphandler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "请勿重复提交订单");
            od.setData(bundle);
            Uphandler.sendMessage(od);
        }
        if (jsonObject.getString("Cop").equals("91")) {//取键码
            Message re = rec .obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "91");
            bundle.putString("code", jsonObject.getString("code"));
            re.setData(bundle);
            rec.sendMessage(re);

        }
        if (jsonObject.getString("Cop").equals("92")) {//投键码
            Message su = sub .obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "92");
            bundle.putString("code", jsonObject.getString("code"));
            su.setData(bundle);
            sub.sendMessage(su);

        }
        if (jsonObject.getString("Cop").equals("93")) {//订单不存在
            Message re = rec .obtainMessage();
            Message su = sub .obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "93");
            re.setData(bundle);
            su.setData(bundle);
            rec.sendMessage(re);
            sub.sendMessage(su);
        }
        if (jsonObject.getString("Cop").equals("94")) {//非法用户
            Message re = rec .obtainMessage();
            Message su = sub .obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "94");
            re.setData(bundle);
            su.setData(bundle);
            rec.sendMessage(re);
            sub.sendMessage(su);
        }
        if (jsonObject.getString("Cop").equals("95")) {//获取失败
            Message re = rec .obtainMessage();
            Message su = sub .obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putString("sentresult", "95");
            re.setData(bundle);
            su.setData(bundle);
            rec.sendMessage(re);
            sub.sendMessage(su);
        }

    }
}






